#include<iostream>
#include<fstream>

#include"contacts.pb.h"

using namespace std;

void AddPersonInfo(contacts2::PersonInfo* person)
{
    std::cout << "-------------新增联系人-------------" << std::endl;

    std::cout << "请输入联系人姓名：";
    std::string name;
    std::getline(std::cin,name);
    person->set_name(name);

    std::cout << "请输入联系人年龄：";
    int age;
    std::cin >> age;
    person->set_age(age);
    //去除换行符
    std::cin.ignore(256,'\n');

    for(int i = 0;;i++){
        std::cout << "请输入联系人电话" << i << "(只输入回车完成电话新增)：";
       std::string number;
        std::getline(std::cin,number);
        if(number.empty()){
            break;
        }

        contacts2::PersonInfo_Phone* phone = person->add_phone();
        phone->set_phone_number(number);

        cout << "请输入该电话的类型(1.移动电话 2.固定电话)：";
        int type;
        cin >> type;
        cin.ignore(256,'\n');
        switch(type){
            case 1:
                phone->set_type(contacts2::PersonInfo_Phone_PhoneType::PersonInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts2::PersonInfo_Phone_PhoneType::PersonInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout << "选择有误!" << endl;
                break;
        }


    }
    contacts2::Address address;
    cout << "请输入联系人家庭住址：";
    string home_address;
    getline(cin,home_address);
    address.set_home_address(home_address);
    cout << "请输入联系人单位地址：";
    string unit_address;
    getline(cin,unit_address);
    address.set_unit_address(unit_address);
    //Address->Any
    person->mutable_data()->PackFrom(address);

    cout << "请选择要添加的其他联系方式(1.qq 2.微信号):";
    int other_contact;
    cin >> other_contact;
    cin.ignore(256,'\n');
    if(1 == other_contact){
        cout << "请输入联系人qq号:";
        string qq;
        getline(cin,qq);
        person->set_qq(qq);
    }else if(2 == other_contact){
        cout << "请输入联系人微信号:";
        string wechat;
        getline(cin , wechat);
        person->set_wechat(wechat);
    }else{
        cout << "选择有误，未成功设置其他联系方式！" << endl;
    }

    for(int i = 0;;i++){
        cout << "请输入备注：" << "标题(只输入回车完成备注新增):";
        string remark_key;
        getline(cin,remark_key);
        if(remark_key.empty()){
            break;
        }

        cout << "请输入备注" << i + 1 << "内容：";
        string remark_value;
        getline(cin,remark_value);
        person->mutable_remark()->insert(remark_key,remark_value);
    }
    
    std::cout << "-----------添加联系人成功-----------" << std::endl;
}

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    contacts2::Contacts contact;

    //读取本地已存在的联系人的信息
    std::fstream istream("contacts.bin",std::ios::in | std::ios::binary);
    if(!istream){
        std::cout << "contacts.bin not found , create a new file!" << std::endl;
    }
    else if(!contact.ParseFromIstream(&istream)){
        std::cerr << "parse false" << std::endl;
        istream.close();
        return -1;
    }

    //向通讯中添加一个联系人
    AddPersonInfo(contact.add_person());

    //将通讯录写入到本地文件中
    std::fstream ostream("contacts.bin",std::ios::out | std::ios::trunc | std::ios::binary);
    if(!contact.SerializeToOstream(&ostream)){
        std::cerr << "write error" << std::endl;
        istream.close();
        ostream.close();
        return -1;
    }

    std::cout << "write success" << std::endl;
    istream.close();
    ostream.close();

    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}
